<?php
namespace service\proxy;

const ROOT_PATH = __DIR__.DIRECTORY_SEPARATOR;

require ROOT_PATH.'curl.php';
require ROOT_PATH.'proxy.php';
require ROOT_PATH.'proxylist.php';
require ROOT_PATH.'redis.php';
require ROOT_PATH.'common_proxy_pool.php';
require ROOT_PATH.'kd100_proxy_pool.php';




class test extends proxy
{

}


$P = proxylist::getInstance('kd100_proxy_pool');

// $P->drop(); //move an ip from good ip pool to a bad ip pool
// $P->block(); //completely remove a ip



// exit;

// $P = new proxylist();
// $P->renew();
echo $P->getIpPoolLength().PHP_EOL;
// var_dump($P->getAll());

// var_dump($P->get());
// echo $P->getLastGetIp();

// var_dump($P->drop($P->getLastGetIp()));

// echo $P->getIpPoolLength();

// var_dump($P->getIpPoolLength(), $P->getDiff());
// exit;

$client = new test();

// $url = 'http://httpbin.org/get?tt=11&aa=123';
// $url = 'http://m.kuaidihelp.com/test';
// $url = 'http://www.kuaidi100.com/query?type=yuantong&postid=560390250666';
$url = 'http://139.196.169.221/appla/index.php?debug=1';
// $url = 'baidu.com';

// $aa = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
// <HTML><HEAD>
// <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=gb2312">
// <TITLE>错误：您所请求的网址（URL）无法获取</TITLE>
// <STYLE type="text/css"><!--BODY{background-color:#ffffff;font-family:verdana,sans-serif}PRE{font-family:sans-serif}--></STYLE>
// </HEAD><BODY>
// <H1>错误</H1>
// <H2>您所请求的网址（URL）无法获取</H2>
// ';

// var_dump(is_string($aa) && stripos($aa, '请求的网址') !== false,stripos($aa, '请求的网址') !== false);

// exit;

// $ip = '10.51.43.253:4433';


// echo date('Y-m-d H:i:s',1472008566.3436);exit;

if(!($ip = $P->get())){
    $P->renew([], false);
}

// echo $ip . PHP_EOL;
// $a = $client->get($url, $ip, [CURLOPT_HEADER => true]) OR $P->drop();
// echo $a;

// //really of mine ip 180.173.94.66

// exit;
// $P->drop();

// http://www.kuaidi100.com/query?type=yuantong&postid=807656982238&id=1&valicode=&temp=0.7215835177886676

// var_dump($P->get(),$P->getIpPoolDiff());
// var_dump($P->getIpPoolLength(), $P->getIpPoolLength(\service\proxy\kd100_proxy_pool::BAD_IP),$P->getIpPoolLength(\service\proxy\kd100_proxy_pool::COMMON_BAD_IP));
// // $P->clearIpPool(false);
// echo $P->getIpPoolLength(false);
// $P->setExpire(false,12);
// echo $P->ttl(false);
// exit;
// var_dump($P->getLastTimeApiReturn());exit;


// if(!($r = $client->get($url,$ip, [CURLOPT_TIMEOUT => 3]))){
//     $P->moveToBlacklist();
//     // $P->drop();
//     echo 'false';
// }
// var_dump(is_string($r),mb_strpos($r, 'charset=gb2312'));
// var_dump(is_string($r),mb_strpos($r, iconv('utf-8','gb2312','您所请求的网址')));


$filter = array_filter([
    '快递公司参数异',
    'Internal Server Error',
    iconv('utf-8','gb2312','您所请求的网址'),
    '您所请求的网址',
    '403 Forbidden',
    'blk_BL_socialnet',
    '快递公司参数异常'
]);

$_patterns = '#'.implode('|', $filter).'#i';
// if(is_string($r) && (bool)preg_match($_patterns, $r)){
//     echo 'falsy';
//     $P->drop();
// }


$urls = [
    // 'http://www.baidu.com',
    // 'http://www.gov.cn',
    // 'http://weibo.com',
    // 'http://qq.com',
    // 'http://www.kuaidi.com/index-ajaxselectcourierinfo-200379498601-yuantong.html',
    // 'http://www.kiees.cn/yto/?wen=882543630433324584&action=ajax&rnd=0.4578285292756865',
    // 'http://biz.trace.ickd.cn/yuantong/882543630433324584?mailNo=882543630433324584&spellName=yuantong&exp-textName=%E5%9C%86%E9%80%9A%E5%BF%AB%E9%80%92&ts=123456&enMailNo=123456789&callback=_jqjsp&_1472094097123=',
    // 'https://www.kuaidi100.com/query?type=yuantong&postid=882543630433324584&id=1&valicode=&temp=0.06873791872429091',
    'http://www.kuaidi100.com/query?type=yuantong&postid=500374765391',
    'https://www.kuaidi100.com/query?type=yuantong&postid=500374765391',
    'http://m.kuaidi100.com/query?type=yuantong&postid=500374765391',

    // 'http://www.kuaidi100.com/query?type=yuantong&postid=500377741269&id=1&valicode=&temp=0.7267860757659386',
    // 'http://m.kuaidi100.com/query?type=yuantong&postid=882612761880232339&id=1&valicode=&temp=0.3281743177057497',
    // 'https://www.kuaidi100.com/query?type=yunda&postid=3903300539521&id=1&valicode=&temp=0.6921651709901022',

    // 'https://www.kuaidi100.com/query?type=jiajiwuliu&postid=733417172',
    // 'http://www.kuaidi100.com/query?type=jiajiwuliu&postid=733417172',
    // 'http://m.kuaidi100.com/query?type=jiajiwuliu&postid=733417172',
];

function repeater($func, $args = [], $callback){
    if(!$r = call_user_func_array($func, $args)){
        return call_user_func_array(__FUNCTION__, func_get_args());
    }
}

$_try_ips = [];
$_try_infos = [];

$max_try_times = 1;

echo date('Y-m-d H:i:s'). PHP_EOL;

foreach ($urls as $key => $url) {
    $i = $max_try_times;
    while((bool)$i--){
        if(!($r = $client->get($url,$ip, [CURLOPT_TIMEOUT => 3]))){
            $_try_infos[$url][] = $_try_ips[] = $ip;
            $P->moveToBlacklist();
            $ip = $P->get();
            echo $ip.' | try times : ',($max_try_times ^ $i), PHP_EOL;
            continue;
        }
        break;
    }
    if(is_string($r) && mb_strlen($r) > 300){
        $r = mb_substr($r, 0, 300);
    }
    if(!$r){
        $r = $client->getLastError();
    }
    $return[$ip][] = [$url, $r];
    if(is_array($r)){
        $r = isset($r['return'])?$r['return']:$r;
    }
    if(is_string($r) && (bool)preg_match($_patterns, $r, $match)){
        echo $ip, $match[0].PHP_EOL;
        $P->drop();
        $ip = $P->get();
    }elseif(is_array($r) && isset(current($r)['error_no'])){
        if((!$skip_404 || current($r)['error_no'] != 56)){
            echo $ip, current($r['error_no']), current($r)['error'], PHP_EOL;
            $ip = $P->get();
        }
    }
    pr($return);
}

function pr(&$data){
    print_r($data);
    $data = null;
    echo date('Y-m-d H:i:s'), microtime(true).PHP_EOL;
}


// echo '<pre>'.PHP_EOL;
// print_r($return);
print_r($_try_infos);
var_dump(count($_try_ips),$_try_ips);
// echo '</pre>'.PHP_EOL;

// if(is_string($r) && (strpos($r, '快递公司参数异') !== false || strpos($r, iconv('utf-8','gb2312','您所请求的网址')) !== false || strpos($r, '您所请求的网址') !== false)){
//     $P->drop();
// }


// $msg = $client->getError();
 // var_dump($r,$P->getLastGetIp(),isset($msg['msg'])?$msg['msg']:'success');

echo $P->getIpPoolLength().PHP_EOL;

//180.173.93.153
